// ignore_for_file: must_be_immutable

import 'package:aitao_flutter/common_ui/empty_view.dart';
import 'package:aitao_flutter/model/hello_text_model.dart';
import 'package:aitao_flutter/utils/log_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import '../request/api.dart';
import '../request/requestManager.dart';
import '../style/color_style.dart';
import '../style/edgeInsets_style.dart';

class GreetTextPage extends StatefulWidget {
  GreetTextPage({super.key, required this.greetTextCallback});
  Function(String) greetTextCallback;
  @override
  State<GreetTextPage> createState() => _GreetTextPageState();
}

class _GreetTextPageState extends State<GreetTextPage> {
  List<HelloTextModel> textList = [];
  @override
  Widget build(BuildContext context) {
    return textList.isEmpty
        ? EmptyView()
        : ListView.builder(
            padding: EdgeInsets.zero,
            itemBuilder: (context, index) {
              return GestureDetector(
                child: Padding(
                    padding: EdgeInsetsStyle.only(top: 10, bottom: 10),
                    child: setHelloTextStyle(textList[index])),
                onTap: () {
                  if (textList[index].isAudit == 0) {
                    EasyLoading.showToast('招呼审核中...');
                    debugPrint('招呼审核中...');
                    return;
                  }
                  widget.greetTextCallback(textList[index].msg ?? '');
                },
              );
            },
            itemCount: textList.length,
          );
  }

  Text setHelloTextStyle(HelloTextModel model) {
    if (model.isAudit == 0) {
      return Text(
        '${model.msg}(审核中)',
        textAlign: TextAlign.center,
        style: const TextStyle(
            fontWeight: FontWeight.bold,
            fontSize: 14,
            color: ColorStyle.aaaaaaColor),
      );
    } else if (model.isAudit == 2) {
      return Text(
        '${model.msg}(审核不通过)',
        textAlign: TextAlign.center,
        style: const TextStyle(
            fontWeight: FontWeight.bold,
            fontSize: 14,
            color: ColorStyle.aaaaaaColor),
      );
    } else {
      return Text(
        '${model.msg}',
        textAlign: TextAlign.center,
        style: const TextStyle(
            fontWeight: FontWeight.bold,
            fontSize: 14,
            color: ColorStyle.zeroColor),
      );
    }
  }

  @override
  void initState() {
    super.initState();
    getTextListData();
  }

  void getTextListData() async {
    var result = await RequestManager().post(
      API.getSayHelloText,
    );
    if (result != null && result['code'] == 0) {
      List tempList = result['data']['list'];
      WJLogUtils.wjLog(result);
      if (tempList.isNotEmpty) {
        setState(() {
          textList = tempList.map((e) {
            return HelloTextModel.fromJson(e);
          }).toList();
        });
      }
    }
  }
}
